package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.bean.Config;
import com.it.mapper.ConfigMapper;
import com.it.page.PageQuery;
import com.it.service.ConfigService;
import com.it.utils.AppUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service("configService")
@RequiredArgsConstructor
public class ConfigServiceImpl implements ConfigService {

    private final ConfigMapper configMapper;

    @Override
    public PageInfo<Config> page(PageQuery<Config> pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());
        Config condition = pageQuery.getQueryBody();
        condition.setDeleted(false);
        List<Config> ds = configMapper.select(condition);
        return new PageInfo<>(ds);
    }

    @Override
    public void delete(Long id) {
        Config config = configMapper.selectByPrimaryKey(id);
        config.setDeleted(true);
        AppUtils.forUpdateAudit(config);
        configMapper.updateByPrimaryKeySelective(config);
    }

    @Override
    public void save(Config config) {
        if (config.getId() == null) {
            AppUtils.forSaveAudit(config);
            configMapper.save(config);
        } else {
            AppUtils.forUpdateAudit(config);
            configMapper.updateByPrimaryKeySelective(config);
        }
    }
}
